package com.hba.init;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.ResourceBundle;

@Configuration
public class RabbitInitConfigure {
    private static final Logger logger = LoggerFactory.getLogger(RabbitInitConfigure.class);
    private static final String DEFAULT_PROPERTIESNAME = "rabbitMq";
    private String propertiesName = DEFAULT_PROPERTIESNAME;

    private String host;
    private String username;
    private String password;
    private String virtualHost;
    private int port;
    private int retryTime;

    @Bean
    protected ConnectionFactory getConectionFactory(){

        ResourceBundle resource = ResourceBundle.getBundle(propertiesName);
        if(StringUtils.isEmpty(host)){
            host = resource.getString("rabbitMq.host");
        }
        if(StringUtils.isEmpty(username) ){
            username = resource.getString("rabbitMq.username");
        }
        if(StringUtils.isEmpty(password) ){
            password = resource.getString("rabbitMq.password");
        }if(StringUtils.isEmpty(virtualHost) ){
            virtualHost = resource.getString("rabbitMq.virtualHost");
        }
        if(port == 0 ){
            port = Integer.parseInt(resource.getString("rabbitMq.port"));
        }if(retryTime == 0 ){
            retryTime = Integer.parseInt(resource.getString("rabbitMq.retryTime"));
        }
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        //connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPort(port);
        connectionFactory.setPublisherConfirms(true);
        connectionFactory.setChannelCacheSize(25);
        return connectionFactory;
    }

    @Bean
    protected AmqpAdmin amqpAdmin(){
        return new RabbitAdmin(getConectionFactory());
    }

    @Bean
    protected RabbitTemplate rabbitTemplate(){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(getConectionFactory());
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if(!ack){
                    logger.error("send message error:" + cause + "correlationData:" + correlationData.toString());
                }
            }
        });

        return rabbitTemplate;
    }

    public int getRetryTime() {
        return retryTime;
    }

    public void setRetryTime(int retryTime) {
        this.retryTime = retryTime;
    }
}
